package com.cgbl.cg.adapter

import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.SpanUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cgbl.cg.R
import com.robot.base.model.ShopCartBean
import com.robot.base.utils.pic.ImageLoaderUtils

class ShoppingCarAdapter :
    BaseQuickAdapter<ShopCartBean, BaseViewHolder>(R.layout.app_item_shoppingcar) {

    private val isEditModel = false

    private val selectPos = arrayListOf<Int>()

    private var pickChange: ((List<ShopCartBean>) -> Unit)? = null

    fun selectAll() {
        selectPos.clear()
        for (i in 0 until data.size) {
            selectPos.add(i)
        }
        doPickChange()
        notifyDataSetChanged()
    }

    fun cancelSelectAll() {
        selectPos.clear()
        doPickChange()
        notifyDataSetChanged()
    }

    override fun convert(holder: BaseViewHolder, item: ShopCartBean) {

//        SpanUtils.with(holder.getView(R.id.tv_name)).append(item.courseInfoDTO?.courseName ?: "")
//            .setLeadingMargin(SizeUtils.dp2px(34f), 0).setFontSize(14, true).create()

        holder.setText(R.id.tv_ori_price, ("￥" + item.courseInfoDTO?.rulingPrice))
            .setText(R.id.tv_price, "￥${item.courseInfoDTO?.coursePrice ?: "0.00"}")
            .setText(R.id.tv_name, item.courseInfoDTO?.courseName ?: "")
        ImageLoaderUtils.displayRound(
            context,
            holder.getView(R.id.iv_cover),
            item.courseInfoDTO?.infoFileRespVOS?.get(0)?.filePath ?: "",
            4
        )
        holder.getView<View>(R.id.fl_checkbox).isSelected =
            selectPos.contains(holder.adapterPosition)

        holder.getView<View>(R.id.fl_checkbox).setOnClickListener {
            val pos = holder.adapterPosition
            if (selectPos.contains(pos)) {
                //取消
                selectPos.remove(pos)
                doPickChange()
                notifyItemChanged(pos)
            } else {
                selectPos.add(pos)
                doPickChange()
                notifyItemChanged(pos)
            }
        }
    }

    fun doPickChange() {
        val pickList = mutableListOf<ShopCartBean>()
        selectPos.forEach {
            pickList.add(data[it])
        }
        pickChange?.invoke(pickList)
    }

    fun setPickChange(pickChange: (List<ShopCartBean>) -> Unit) {
        this.pickChange = pickChange
    }
}